﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BasicDataManagement;
using DataModel;

namespace Preproduce
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        IOpertation op;
        //WPFClient.Window1 win;

        public MainWindow()
        {
            InitializeComponent();
            op = OperationFactory.Create();
            //win = new WPFClient.Window1();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dataGridPreproduce.ItemsSource = op.LoadPreproduceList();
            //nameColumn.ItemsSource = new ObservableCollection<string>(op.LoadFixedDishItemName());
            //win.Connect("System");
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            var source = dataGridPreproduce.ItemsSource as List<PreproduceObject>;
            op.UpdatePreproduceList(source);
            List<string> tmpDishes = new List<string>();
            foreach (var t in source)
            {
                tmpDishes.Add(t.FixedDishName + "（" + t.Value + "份）");
                op.AddKitchenBillItem(0, "预约成品：" + t.FixedDishName + "（" + t.Value + "份）");
            }
            //SystemWindow.SVC.Message msg = new SystemWindow.SVC.Message()
            //{
            //    Sender = "系统",
            //    Content = "预约成品",
            //    Time = DateTime.Now,
            //    Dishes = tmpDishes
            //};
            //win.Connect("System");
            //win.Send(msg, "System");
            MessageBox.Show("预约发送成功！");
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            AddPreproduceWindowxaml win = new AddPreproduceWindowxaml();
            win.Owner = this;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            if (win.ShowDialog() == true)
            {
                var source = dataGridPreproduce.ItemsSource as List<PreproduceObject>;
                source.Add(win.pre);
                dataGridPreproduce.ItemsSource = null;
                dataGridPreproduce.ItemsSource = source;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //win.Disconnect();
            //win.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            if (dataGridPreproduce.SelectedItem != null)
            {
                var source = dataGridPreproduce.ItemsSource as List<PreproduceObject>;
                source.Remove(dataGridPreproduce.SelectedItem as PreproduceObject);
                dataGridPreproduce.ItemsSource = null;
                dataGridPreproduce.ItemsSource = source;
            }
        }
    }
}
